import React, { PureComponent } from 'react'

//1.
import { EventEmitter } from 'events'; 

//2.创建事件总线
const eventBus = new EventEmitter()

class Home extends PureComponent {

  //4. 接收event 并在组件卸载是 移除
  componentDidMount() {
    eventBus.addListener('sayHello', this.handlerSayHelloEvent )
  }
  // componentDidMount() {
  //   eventBus.addListener('sayHello', (args) => {

  //   })
  // }

  componentWillUnmount() {
    eventBus.removeListener('sayHello', this.handlerSayHelloEvent )
  }

  handlerSayHelloEvent(msg, num, count) {
    console.log(msg, num, count);
  }

  render() {
    return (
      <div>
        Home
      </div>
    )
  }
}

class Profile extends PureComponent {
  render() {
    return (
      <div>
        Profile
        <button onClick={e => this.eventChange() }>发送事件</button>
      </div>
    )
  }
  //3.发送事件
  eventChange() {
    eventBus.emit('sayHello', 'hello react', 123, 321)
  }
}


export default class App extends PureComponent {
  render() {
    return (
      <div>
        <h2>App</h2>
      
        
        <hr />
        <Profile />
        <Home />
      </div>
    )
  }
}
